﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DelonixRegiaHotel.Classes;
using DelonixRegiaHotel.DBManager;

namespace DelonixRegiaHotel
{
    public partial class AdditionalCheckInItems : Form
    {

        ArrayList getItems = new ArrayList();
        ArrayList tempItems = new ArrayList();
        decimal unitSubPrice;
        decimal totalUnitSubPrice;
        decimal totalAmount;
        public AdditionalCheckInItems()
        {

            InitializeComponent();
            labelChkIn.Visible = false;
            labelCusID.Visible = false;
            labelCusName.Visible = false;
            labelRoomNo.Visible = false;
            tbxCheckID.Visible = false;
            tbxCustomerID.Visible = false;
            tbxCustomerName.Visible = false;
            cboxRoomNo.Visible = false;
            getItems = DelonixRegiaDB.getAllItems();
            dataGridView1.DataSource = getItems;
            dataGridView1.Columns[0].ReadOnly = true;
            dataGridView1.Columns[1].ReadOnly = true;


            DataGridViewTextBoxColumn myTextBoxColum = new DataGridViewTextBoxColumn();
            {
                Name = "Quantity";
                myTextBoxColum.HeaderText = "Quantity";
                //DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
                //cellStyle.BackColor = Color.AntiqueWhite;
                //myTextBoxColum.DefaultCellStyle = cellStyle;
                Visible = true;
            };
            dataGridView1.Columns.Add(myTextBoxColum);


            DataGridViewCheckBoxColumn myCheckedColumn = new DataGridViewCheckBoxColumn()
            {
                Name = "Checked Items",
                FalseValue = 0,
                TrueValue = 1,

                Visible = true


            };
            // add the new column to your dataGridView 
            dataGridView1.Columns.Add(myCheckedColumn);


        }

        private void btnAdd_Click(object sender, EventArgs e)
        {


            int rowInsert = 0;
            int rowUpdate = 0;

            bool selected = false;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                bool cheked = ((bool)(row.Cells["Checked Items"].EditedFormattedValue));
                if (cheked)
                {
                    selected = true;
                }

            }
            if (!selected)
            {
                MessageBox.Show("Please select at least one Item !!");
            }

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {

                if (Convert.ToBoolean(row.Cells["Checked Items"].Value))
                {

                    string checkID = tbxCheckID.Text;
                    string itemName = row.Cells[0].Value.ToString();
                    if (row.Cells[2].Value == null)
                    {
                        row.Cells[2].Value = 1;
                    }
                    int quantity = Convert.ToInt32(row.Cells[2].Value.ToString());

                    unitSubPrice = Convert.ToInt32(row.Cells[2].Value.ToString()) * Convert.ToDecimal(row.Cells[1].Value.ToString());

                    bool checkForExisitingItems = DelonixRegiaDB.CheckForExistingItem(itemName, checkID);
                    bool checkForCheckID = DelonixRegiaDB.CheckForExistingCheckID(checkID);

                    if (checkForExisitingItems == false)
                    {
                        CheckOutExtraCost coe = new CheckOutExtraCost();
                        
                        coe.CheckID = checkID;
                        coe.ItemName = itemName;
                        coe.Quantity = quantity;
                        coe.UnitSubTotal = unitSubPrice;

                        rowInsert = DelonixRegiaDB.InsertCheckOutExtraCost(coe);

                    }
                    else
                    {
                        if (checkForCheckID == true)
                        {

                            CheckOutExtraCost coe1 = DelonixRegiaDB.getItemsFromChkIn(checkID, row.Cells[0].Value.ToString());
                            coe1.CheckID = checkID;
                            coe1.ItemName = coe1.ItemName;
                            coe1.Quantity = coe1.Quantity + quantity;
                            coe1.UnitSubTotal = coe1.UnitSubTotal + unitSubPrice;
                            if (Convert.ToString(quantity) == string.Empty)
                            {
                                MessageBox.Show("Not inserted!");
                            }
                            rowUpdate = DelonixRegiaDB.UpdateExistingItems(coe1);

                        }

                    }
                }//end of if cell is empty


                //} //end of if row.Cells != null


            }   //end of for eachloop

            ArrayList temp1 = new ArrayList();
            temp1 = DelonixRegiaDB.getAllunitSubTotal(tbxCheckID.Text);
            foreach (var c in temp1)
            {
                if (c is CheckOutExtraCost)
                {
                    CheckOutExtraCost coec = new CheckOutExtraCost();
                    coec = (CheckOutExtraCost)c;
                   totalAmount = coec.UnitSubTotal;
                   totalUnitSubPrice += totalAmount;
                }

                
            }
            MessageBox.Show("" + totalUnitSubPrice);
            CheckInOut cio1 = new CheckInOut();
            cio1.CheckID = tbxCheckID.Text;
            cio1.ExtraCostTotal = totalUnitSubPrice;
            DelonixRegiaDB.UpdateTotalCost(cio1);
            


            if (rowInsert == 1 || rowUpdate == 1)
            {
                MessageBox.Show("Successfully Added!", "Success!", MessageBoxButtons.OK);
                this.Close();
            }
            else
            {
                MessageBox.Show("Error Please Ensure that all information is filled in!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }


        }
        private void chkboxes_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            if (!chk.Checked)
                chk.Checked = true;
            else
                chk.Checked = false;
        }


        private void btnCheck_Click(object sender, EventArgs e)
        {
 
            labelChkIn.Visible = true;
            labelCusID.Visible = true;
            labelCusName.Visible = true;
            labelRoomNo.Visible = true;
            tbxCheckID.Visible = true;
            tbxCustomerID.Visible = true;
            tbxCustomerName.Visible = true;
            cboxRoomNo.Visible = true;
            ArrayList temp = new ArrayList();
            cboxRoomNo.Items.Clear();
            temp = DelonixRegiaDB.GetcheckInDetails(tbxChkNric.Text);

            foreach (var c in temp)
            {
                if (c is Customer)
                {
                    Customer cu = new Customer();
                    cu = (Customer)c;
                    tbxCustomerID.Text = cu.CustomerID;
                    string firstName = cu.FirstName;
                    string lastName = cu.LastName;
                    tbxCustomerName.Text = firstName + " " + lastName;
                }
                if (c is CheckInOut)
                {
                    CheckInOut cio = new CheckInOut();
                    cio = (CheckInOut)c;
                    //         tbxRoomNo.Text = cio.RoomNo;
                    cboxRoomNo.Items.Add(cio.RoomNo);
                    //        tbxCheckID.Text = cio.CheckID;

                }
            }



        }

        private void cboxRoomNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            string roomNo = (string)cboxRoomNo.SelectedItem;

            CheckInOut cio = DelonixRegiaDB.getCheckInID(roomNo);
            tbxCheckID.Text = cio.CheckID;

        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }




    }
}
